<?php
/**
 * Created by PhpStorm.
 * User: lixiaohui
 * Date: 2015/9/21
 * Time: 22:25
 */

namespace Home\Controller;


use Think\Controller;
use Think\Log;

/**
 * Class OfficeController
 * @package Home\Controller 微信办公控制器 面向员工的服务
 */
class OfficeController extends Controller
{
    //首页
    public function index()
    {
        $openId = getOpenId();
        $memberModel = M('wx_member_info') -> where("openid='%s'",$openId) -> field('headimgurl,name,phone') ->find();

        //查询是否是员工
        $args = array(
            "name"=>$memberModel['name'],
            "tel"=>$memberModel['phone']
        );
        $postString = getPostString('GetRy',$args);
        $retStr = post(C('INTERFACE_BASE_URL'),$postString);
        Log::write(C('INTERFACE_BASE_URL').'?'.$postString);
        $retObj = json_decode($retStr,true);
        //dump($retObj);
        if(intval($retObj['retcode']) != 0){
            $this -> assign("Worker",array("status"=>"0"));
        } else{
            $this -> assign("Worker",array("status"=>"1"));
        }
        $sub_button = "微信办公";
        $sub_sub_button = array(
            array("url"=>"/?m=Home&c=Office&a=stop","sub_sub_button"=>"停水登记"),
            array("url"=>"/?m=Home&c=Office&a=stopSearch","sub_sub_button"=>"停水查询"),
            array("url"=>"/?m=Home&c=Office&a=reuse","sub_sub_button"=>"复水登记"),
            array("url"=>"/?m=Home&c=Office&a=feeSearch","sub_sub_button"=>"缴费查询"),
            array("url"=>"/?m=Home&c=Office&a=usedSearch","sub_sub_button"=>"用水查询"),
            array("url"=>"/?m=Home&c=Office&a=preSaveSearch","sub_sub_button"=>"预存对账")
        );
        //dump($sub_sub_button);
        $this->assign("menu", $sub_sub_button);
        $this->assign("memberModel", $memberModel);
        $this->assign("bottomMenu", getBottomMenu());
        $this->display();
    }

    //停水登记视图
    public function stop()
    {
        //获取停水类别
        $postString = getPostString('GetStopType',array("foo"=>"foo"));
        //dump($postString);
        $retStr = post(C('INTERFACE_BASE_URL'),$postString);
        $retObj = json_decode($retStr,true);
        $this -> assign('stoptype',$retObj['data']['list']);
        //dump($retObj['data']['list']);
        $this->display();
    }

    //停水登记 ajax
    public function stopRegister()
    {
        $userId = I('user-id');
        $stopType = I('stop-type');
        $reason = I('reason');
        Log::write("userid=$userId,stoptype=$stopType");
        $args = array(
            "userid" => $userId,
            "type_id" => 0,
            "stoptypeid" => $stopType,
            "bz"=>$reason
        );

        $postString = getPostString('StopWater',$args);
        $retStr = post(C('INTERFACE_BASE_URL'),$postString);
        Log::write(C('INTERFACE_BASE_URL').'?'.$postString);
        $retObj = json_decode($retStr,true);
        if(intval($retObj['retcode']) == 0){//获取成功
            $this -> ajaxReturn(array("status"=>"success","data"=>$retObj));
        } else{
            $this -> ajaxReturn(array("status"=>"fail","data"=>$retObj));
        }

    }

    //停水搜索视图
    public function stopSearch()
    {
        $this->display();
    }

    //复水登记视图
    public function reuse()
    {
        $this->display();
    }
    //复水登记 ajax
    public function reuseRegister(){
        $userId = I('userId');
        $reason = I('reason');
        Log::write("userId=".$userId.",reason=".$reason);
        $args = array(
            "userid" => $userId,
            "type_id" => 2,//1停水，2复水
            "stoptypeid" => '',
            "bz" => $reason
        );
        $postString = getPostString('StopWater',$args);
        Log::write(C('INTERFACE_BASE_URL').'?'.$postString);
        $retStr = post(C('INTERFACE_BASE_URL'),$postString);
        $retObj = json_decode($retStr,true);
        if(intval($retObj['retcode']) == 0){
            $this -> ajaxReturn(array("status"=>"success","data"=>$retObj));
        } else{
            $this -> ajaxReturn(array("status"=>"fail","data"=>$retObj));
        }
    }

    //获取停水列表 ajax
    public function stopList(){
        $userId = I('userId');
        $args = array(
            "userid"=>$userId
        );
        $retStr = post(C('INTERFACE_BASE_URL'),getPostString('GetStopList',$args));
        Log::write("retStr=$retStr");
        $retObj = json_decode($retStr,true);
        if(intval($retObj['retcode']) == 0){//获取成功
            $this -> ajaxReturn(array("status"=>"success","data"=>$retObj));
        }else {
            $this -> ajaxReturn(array("status"=>"fail","data"=>$retObj));
        }
    }

    //用水查询首页
    public function usedSearch()
    {
        $this->display();
    }

    //用水查询结果页
    public function usedSearchResult()
    {
        $userid = I('userid');
        $year = I('year');
        $args = array(
            "userid" => $userid,
            "queryyear" => $year
        );
        //判断用户是否存在
        if (!isUserExist(1, $userid)) {
            $retObj = array(
                "retcode" => -1,
                "msg" => "用户 '$userid' 不存在",
                "userid" => $userid,
                "year" => $year
            );
            $this->assign("json", $retObj);
            $this->display();
            return;
        }
        //调用接口进行用水查询
        $retObj = doQuery('QueryUserWaterLog', $args);
        $this->assign("json", $retObj);
        $this->display();
    }

    //缴费查询
    public function feeSearch()
    {
        $this->display();
    }

    //缴费查询结果
    public function feeSearchResult()
    {
        $userid = I('userid');
        $year = I('year');
        $args = array(
            "userid" => $userid,
            "queryyear" => $year
        );
        //判断用户是否存在
        if (!isUserExist(1, $userid)) {
            $retObj = array(
                "retcode" => -1,
                "msg" => "用户 '$userid' 不存在",
                "userid" => $userid,
                "year" => $year
            );
            $this->assign("json", $retObj);
            $this->display();
            return;
        }
        //调用接口进行缴费查询
        $retObj = doQuery('QueryRechargeLog', $args);
        $this->assign("json", $retObj);
        $this->display();
    }

    //预存对账查询
    public function preSaveSearch()
    {
        $this->display();
    }

    //预存查询结果
    public function preSaveSearchResult()
    {
        $userid = I('userid');
        $year = I('year');
        $args = array(
            "userid" => $userid,
            "queryyear" => $year
        );
        //判断用户是否存在
        if (!isUserExist(1, $userid)) {
            $retObj = array(
                "retcode" => -1,
                "msg" => "用户 '$userid' 不存在",
                "userid" => $userid,
                "year" => $year
            );
            $this->assign("json", $retObj);
            $this->display();
            return;
        }
        //调用接口进行预存查询
        $retObj = doQuery('QueryAccount', $args);
        $this->assign("json", $retObj);
        $this->display();
    }
}